package com.lzx.iteam.net;

import android.os.Message;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.lzx.iteam.bean.SalaryGroupBean;
import com.lzx.iteam.bean.SalaryRuleBean;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by xuepengju on 14-3-20.
 */
public class GetSalaryRuleMsg extends CcMsgStructure {

    private Message mCallback;

    public GetSalaryRuleMsg(Message callback){
        mCallback = callback;
    }

    @Override
    public void onReceive(JSONObject data) {
        SalaryRuleBean bean = null;
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            bean = new Gson().fromJson(jsonObject.getString("group_rule"), SalaryRuleBean.class);
            JSONObject obj = jsonObject.getJSONObject("group_rule");

            /**
             * 当 值为空时表示没有设置，这里暂且置为-1，
             * 获取数据时判断 -1就取默认值
             */

            if (!obj.has("p_pension_pc")){
                bean.setP_pension_pc(-1);
            }
            if (!obj.has("p_medical_pc")){
                bean.setP_medical_pc(-1);
            }
            if (!obj.has("p_medical_count")){
                bean.setP_medical_count(-1);
            }
            if (!obj.has("p_provident_fund_pc")){
                bean.setP_provident_fund_pc(-1);
            }
            if (!obj.has("p_unemployment_pc")){
                bean.setP_unemployment_pc(-1);
            }


            if (!obj.has("c_pension_pc")){
                bean.setC_pension_pc(-1);
            }
            if (!obj.has("c_medical_pc")){
                bean.setC_medical_pc(-1);
            }
            if (!obj.has("c_provident_fund_pc")){
                bean.setC_provident_fund_pc(-1);
            }
            if (!obj.has("c_unemployment_pc")){
                bean.setC_unemployment_pc(-1);
            }
            if (!obj.has("c_injury_pc")){
                bean.setC_injury_pc(-1);
            }
            if (!obj.has("c_birth_pc")){
                bean.setC_birth_pc(-1);
            }

        } catch (JSONException e) {
//            e.printStackTrace();
            Log.d("My_Exception",e.toString());
        }
        if (mCallback != null)
        {
            mCallback.obj = bean;
            mCallback.arg1 = 0;
            mCallback.sendToTarget();
            return;
        }
    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mCallback != null){
            mCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mCallback.obj = errorMsg;
            }else{
                mCallback.obj = Constants.NETWORK_WRROR;
            }
            mCallback.sendToTarget();
        }
    }
}
